package com.laijiale.controller;

import cn.hutool.core.bean.BeanUtil;
import com.laijiale.Service.EmpService;
import com.laijiale.Service.RoleService;
import com.laijiale.dto.*;
import com.laijiale.entities.Role;
import com.laijiale.excel.RoleExcel;
import com.laijiale.excel.RoleExcel;
import com.laijiale.result.Result;
import com.laijiale.util.EasyExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author laijiale
 * @date 2024年07月28日 11:59
 */
@Tag(name = "角色控制器")
@RestController
@RequestMapping("/api/v1/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private EmpService empService;
    @Operation(summary = "添加一条记录")
    @PostMapping("save")
    public Object save(@RequestBody  @Validated RoleInsertDTO roleInsertDTO){

        return roleService.insert(roleInsertDTO);
    }
    @Operation(summary = "查询一条记录")
    @GetMapping("getInfo/{id}")
    public Object getInfo(@PathVariable Long id){
        return roleService.selectById(id);
    }
    @Operation(summary = "修改一条记录")
    @PostMapping("update")
    public Object update(@RequestBody @Validated RoleUpdateDTO roleUpdateDTO){

        return roleService.updateById(roleUpdateDTO);

    }
    @Operation(summary = "删除一条记录")
    @PostMapping("remove/{id}")
    public Object remove(@PathVariable Long id){

        return roleService.logicDeleteById(id);

    }
    @Operation(summary = "批量删除记录")
    @PostMapping("removeBatch")
    public Object removeBatch(@RequestParam List<Long> ids){

        return roleService.logicDeleteByIds(ids);

    }
    @Operation(summary = "全查询记录")
    @GetMapping("list")
    public Object list(){

        return roleService.list();

    }
    @Operation(summary = "分页查询记录")
    @PostMapping("page")
    public Object page(@RequestBody  @Validated PageDTO pageDTO){

        return roleService.page(pageDTO);

    }

    @SneakyThrows
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp) {
        List<RoleExcel> roleExcels = new ArrayList<>();
        List<Role> roles = roleService.list();
        for (Role role: roles) {
            RoleExcel roleExcel = BeanUtil.copyProperties(role, RoleExcel.class);
            roleExcels.add(roleExcel);
        }
        EasyExcelUtil.download(resp, "角色统计表.xlsx", "sheet01",
                RoleExcel.class, roleExcels);
    }
    @Operation(summary = "我的权限列表")
    @PostMapping("getPermissions/{id}")
    public Object getPermissions(@PathVariable Long id){
        return roleService.getPermissions(id);
    }
    @Operation(summary = "修改权限")
    @PostMapping("updatePermissions")
    public Object updatePermissions(@RequestBody @Validated RoleUpdatePermissionsDTO dto) {
        return roleService.updatePermissions(dto);
    }
    @Operation(summary = "按角色名搜索")
    @PostMapping("pageLikeTitle")
    public Object pageLikeTitle(@RequestBody @Validated RolePageDTO dto){
        return roleService.pageLikeTitle(dto);
    }


}
